import 'dart:io';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutterapp1/model/ATVideo.dart';
import 'package:flutterapp1/model/BaseModel.dart';
import 'dart:convert' as convert;
import 'MyEncrypt.dart';

const AppBaseUrl1  = "https://api.zhuishushenqi.com";
const AppBaseUrl2  = "http://api.akousn.com";
const AppBaseUrl3  = "https://mjappaz.yefu365.com";
enum HttpMethod{
  Get,
  Post,
}
class MyNetTool{
    static Future<Map> method(HttpMethod method,String url,String txCode,Map bodys,Map header) async{
    var stringUrl = url + txCode;
    switch (method){
      case HttpMethod.Get:{
        try{
          Dio dio = new Dio();
          if(header != null){
            var headers = new Map<String, dynamic>.from(header);
            BaseOptions options =  BaseOptions(headers:headers);
            dio.options = options;
          }
          var params = bodys != null ? new Map<String, dynamic>.from(bodys) : bodys;
          Response response = await dio.get(stringUrl,queryParameters: params);
          var code = response.statusCode;
          print(response.statusCode);
          if (code == 200){
            Map<String ,dynamic> object = json.decode(response.toString());
            print(object);
            return object;
          }else{
            return {};
          }
        }catch (e){
          return {};
          print(e);
        }
      }break;
      case HttpMethod.Post:{
        try{
          Dio dio = new Dio();
          if(header != null){
            var headers = new Map<String, dynamic>.from(header);
            BaseOptions options = BaseOptions(headers:headers);
            dio.options = options;
          }
          var params = bodys != null ? new Map<String, dynamic>.from(bodys) : bodys;
          Response response = await dio.post(stringUrl,data:params);
          var code = response.statusCode;
          if(code == 200){
            Map<String ,dynamic> object = json.decode(response.data.toString());
            return await object;
          }else{
            return {};
          }
        }catch (e){
          return {};
        }

      }break;

    }
  }
}

class  ATNetVideo{
  static Future<Map> index(String txCode,Map map) async{
    Map params = await MyEncrypt.encryptionParams(map, EncryptionState.encryption);
    print(params);
    Map<String ,dynamic> object = await MyNetTool.method(HttpMethod.Post, AppBaseUrl2, "/index/index", params, MyEncrypt.headers());
    try{
      BaseModel model = BaseModel.fromJson(object);
      if (model.code == 2){
        String decode = await MyEncrypt.decodeString(model.data);
        Map<String, dynamic> jsonString = convert.jsonDecode(decode);
        return jsonString;
      }else{
        Map<String, dynamic> jsonString = convert.jsonDecode(model.data);
        return jsonString;
      }
    }catch (e){
      return null;
    }
  }
}

class  ATNetVideoTool{
  static Future<ATHomeVideo> home(String vsize) async{
    Map map = new Map();
    map.addAll({"vsize":vsize});
    Map<String ,dynamic> object = await baseNetTool("/index.php/app/ios/topic/index", map,);
    try {
      ATHomeVideo video = ATHomeVideo.fromJson(object);
      print(video.data);
      return video;
    }catch (e){
      print(e);
      return null;
    }
  }
  static Future<Map> movie(String movieId,String vsize) async{
    Map map = new Map();
    map.addAll({"vsize":vsize});
    map.addAll({"movieId":movieId});
    Map<String ,dynamic> object = await baseNetTool("/index.php/app/ios/type/index", map,);
  }
  static Future<Map> homeMore(int page,int size,String ztid) async{
    Map map = new Map();
    map.addAll({"page":page});
    map.addAll({"size":size});
    map.addAll({"ztid":ztid});
    Map<String ,dynamic> object = await baseNetTool("/index.php/app/ios/vod/index", map,);
    return object;
  }
  static Future<Map> movieMore(int page,int size,String movieId) async{
    Map map = new Map();
    map.addAll({"page":page});
    map.addAll({"size":size});
    map.addAll({"movieId":movieId});
    Map<String ,dynamic> object = await baseNetTool("/index.php/app/ios/vod/index", map,);
    return object;
  }
  static Future<Map> search(int page,int size,String keyWord) async{
    Map map = new Map();
    map.addAll({"page":page});
    map.addAll({"size":size});
    map.addAll({"keyWord":keyWord});

    Map<String ,dynamic> object = await baseNetTool("/index.php/app/ios/vod/index", map,);
    return object;
  }
  static Future<Map> show(String movieId) async{
    Map map = new Map();
    map.addAll({"movieId":movieId});

    Map<String ,dynamic> object = await baseNetTool("/index.php/app/ios/vod/show", map,);
    return object;
  }

  static Future<Map> baseNetTool(String txCode,Map map) async{
    Map<String ,dynamic> object = await MyNetTool.method(HttpMethod.Get, AppBaseUrl3, txCode, map,headers());
    return object;
    print("=====================");
    print(object);
    try{
      BaseModel model = BaseModel.fromJson(object);
      print(model.data);
      print(model.code);
      if(model.code == 0){
        Map<String, dynamic> jsonString = convert.jsonDecode(model.data);
        return jsonString;
      }else{
        return null;
      }
    }catch (e){
      print(e);
      return null;
    }
  }
  static Map headers(){
    var map = Map();
    map.addAll({"Accept":"*/*"});
    map.addAll({"Accept-Encoding":"br, gzip, deflate"});
    map.addAll({"Accept-Language":"en-CN;q=1, zh-Hans-CN;q=0.9"});
    map.addAll({"Connection":"keep-alive"});
    map.addAll({"Content-Type":"application/x-www-form-urlencoded;charset=utf8"});
    map.addAll({"Host":"mjappaz.yefu365.com"});
    return map;
  }
}